// miniprogram/pages/home/vitae/vitae.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vitaeId:null,
    deliveryId:null,
    uniqueId:null,
    avatarUrl: './user-unlogin.png',
    title:"",
    name:"余思彬",
    sex:"男",
    age:"22",
    address:"广东广州",
    phone:"13723456203",
    email:"v2tila@qq.com",
    schoolName:"",
    collegeName:"医药信息工程学院",
    className:"计算机科学与技术152",
    files:[],
    isPreview:0,
    swiperCurrent:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.isPreview==1){
      let {
        title,
        avatarUrl,
        sex,
        phone,
        email,
        address,
        birthday,
        intro,
        files
      } = options;
      files = JSON.parse(files);
      that.setData({
        avatarUrl: avatarUrl,
        title:title,
        name: app.globalData.studentInfo.studentName,
        sex:sex,
        birthday:birthday,
        phone:phone,
        email:email,
        intro: intro,
        schoolName: app.globalData.studentInfo.schoolName,
        collegeName: app.globalData.studentInfo.collegeName,
        className: app.globalData.studentInfo.className,
        files:files,
        isPreview:options.isPreview
      })
    }else{
      wx.request({
        url: app.globalData.url+'/vitae/getVitaeDetail',
        data:{
          id:options.id
        },
        success:res=>{
          var data = res.data.vitaeVO
          console.log(data.vitaeImgs)
          that.setData({
            vitaeId:options.id,
            deliveryId:options.deliveryId,
            uniqueId:data.uniqueId,
            name: data.name,
            sex: data.sex,
            birthday: data.birthday,
            phone: data.phone,
            address:data.address,
            email: data.email,
            schoolName: data.schoolName,
            collegeName: data.collegeName,
            className: data.className,
            intro: data.intro,
            vitaeImgs: data.vitaeImgs,
            avatarUrl:data.avatarUrl,
            files:JSON.parse(data.vitaeImgs),
            isPreview:options.isPreview,
          })
        }
      })
    }
    
  },
  previewAvatarUrl: function () {
    wx.previewImage({
      current: this.data.avatarUrl, // 当前显示图片的http链接 
      urls: [this.data.avatarUrl]  
    })
  },
  swiperChange: function (e) {  
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  previewImage: function (e) {
    console.log(this.data.files[this.data.swiperCurrent])
    wx.previewImage({
      current: this.data.files[this.data.swiperCurrent], // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  submitButton:function(){
    var that = this
    wx.showModal({
      title: '提示',
      content: "确认保存",
      success: function (res) {
        if (res.confirm) {
          that.saveVitae();
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  sendButton: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: "确认发送",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../message/template/choose?uniqueId='+that.data.uniqueId+
              "&deliveryId=" + that.data.deliveryId
          })
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  saveVitae:function(){
    var that = this
    var data = that.data
    wx.showLoading({
      title: '上传中',
    })
    if(data.files.length==0){
      var filePath = data.avatarUrl
      var cloudPath = 'vitaeAva/' +
        data.schoolName + "/" +
        data.collegeName + "/" +
        data.className + "/" +
        data.name + "/" + data.title +
        filePath.match(/\.[^.]+?$/)[0]
      wx.cloud.uploadFile({
        cloudPath,
        filePath,
        success: res => {
          console.log('[上传文件] 成功：', res)
          console.log('[上传文件] cloudPath：', cloudPath)
          console.log('[上传文件] filePath', filePath)
          var avatarUrl = res.fileID
          wx.hideLoading()
          wx.request({
            url: app.globalData.url + '/vitae/save',
            data: {
              title: data.title,
              uniqueId: app.globalData.uniqueId,
              name: data.name,
              avatarUrl: avatarUrl,
              sex: data.sex,
              birthday: data.birthday,
              phone: data.phone,
              email:data.email,
              address: data.address,
              intro: data.intro,
              schoolName: data.schoolName,
              collegeName: data.collegeName,
              className: data.className,
            },
            success: res => {
              if (res.data.code == 1) {
                wx.showToast({
                  title: '保存成功'
                });
                wx.redirectTo({
                  url: '../my/vitae',
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                })
              }
            }
          })
        },
      })
    }else{
      var fileIDs = [];
      for (var i = 0; i < data.files.length; i++) {
        const filePath = data.files[i]
        const cloudPath = 'vitaeImgs/' +
          data.schoolName + "/" +
          data.collegeName + "/" +
          data.className + "/" +
          data.name + "/" + data.title + "/" +
          i + filePath.match(/\.[^.]+?$/)[0]
        console.log(cloudPath)
        console.log(filePath)
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)
            console.log('[上传文件] cloudPath：', cloudPath)
            console.log('[上传文件] filePath', filePath)
            fileIDs.push(res.fileID)
            console.log(fileIDs)
          },
          complete: res => {
            if (data.files.length == fileIDs.length) {
              console.log(JSON.stringify(fileIDs))
              var filePath = data.avatarUrl
              var cloudPath = 'vitaeAva/' +
                data.schoolName + "/" +
                data.collegeName + "/" +
                data.className + "/" +
                data.name + "/" + data.title +
                filePath.match(/\.[^.]+?$/)[0]
              wx.cloud.uploadFile({
                cloudPath,
                filePath,
                success: res => {
                  console.log('[上传文件] 成功：', res)
                  console.log('[上传文件] cloudPath：', cloudPath)
                  console.log('[上传文件] filePath', filePath)
                  var avatarUrl = res.fileID
                  wx.hideLoading()
                  wx.request({
                    url: app.globalData.url + '/vitae/save',
                    data: {
                      title: data.title,
                      uniqueId: app.globalData.uniqueId,
                      name: data.name,
                      avatarUrl: avatarUrl,
                      sex: data.sex,
                      birthday: data.birthday,
                      phone: data.phone,
                      email: data.email,
                      address: data.address,
                      intro: data.intro,
                      schoolName: data.schoolName,
                      collegeName: data.collegeName,
                      className: data.className,
                      vitaeImgs: JSON.stringify(fileIDs)
                    },
                    success: res => {
                      if (res.data.code == 1) {
                        wx.showToast({
                          title: '保存成功'
                        });
                        wx.redirectTo({
                          url: '../my/vitae',
                        })
                      }else{
                        wx.showModal({
                          title: '提示',
                          content: res.data.msg,
                        })
                      }
                    }
                  })
                },
              })
            }
          }
        })
      } 
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})